<?php

namespace app\common\lib;

class Arr
{
    /**
     * 树状数据，支持无限极分类
     */
    public static function getTree($data,$pid='pid',$child='child',$select='perms',$selectData=[]): array
    {
        $items = [];
        foreach ($data as $v) {
            $items[$v['id']] = $v;
        }
        $tree = [];
        foreach ($items as $id => $item) {
            $items[$id]['checked']=false;
            if(isset($item[$select])&&in_array($item[$select],$selectData)){
                $items[$id]['checked']=true;
            }
            if (isset($items[$item[$pid]])) {
                $items[$item[$pid]][$child][] = &$items[$id];
            } else {
                $tree[] = &$items[$id];
            }
        }
        return $tree;
    }

    public static function toNumber($data){
        foreach ($data as $k=>$v){
            $data[$k]=$v*1;
        }
        return $data;
    }

    public static function arrSort($arr,$by,$sort=SORT_ASC){
        $col=array_column($arr,$by);
        array_multisort($col,$sort,$arr);
        return $arr;
    }

    public static function arrDic($arr,$by){
        $data=[];
        foreach ($arr as $k=>$v){
            $data[$v[$by]]=$v;
        }
        return $data;
    }
}
